package com.tbit.uqbike.webmanager.business.maintain_performance.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tbit.common.constant.PerformanceTypeConstant;
import com.tbit.common.entity.view.PageResult;
import com.tbit.config.datasource.DataSource;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.*;
import com.tbit.uqbike.object.pojo.statistics.PowerChangeLog;
import com.tbit.uqbike.object.pojo.statistics.PowerLogDetail;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.ParkPointService;
import com.tbit.uqbike.service.base.RedisService;
import com.tbit.uqbike.service.business.AccountUserService;
import com.tbit.uqbike.service.business.PowerChangeMonitorConfigService;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceBizType;
import com.tbit.uqbike.webmanager.business.maintain_performance.constant.PerformanceRuleType;
import com.tbit.uqbike.webmanager.business.maintain_performance.dao.log.PowerChangePerformanceDao;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PowerChangePerformance;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.PowerChangePerformanceLog;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.config.PowerChangePerformanceConfig;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.rule.LowPowerRule;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.rule.PowerAfterChangeRule;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.rule.StationPriorityBasedRule;
import com.tbit.uqbike.webmanager.business.maintain_performance.object.dto.stat.*;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 换电绩效记录服务
 *
 * @author <a href="mailto:zheng.zebiao@tbit.com.cn">zb</a>
 * @since 2025/2/18
 */
@Slf4j
@Service
@AllArgsConstructor
public class PowerChangePerformanceService extends ServiceImpl<PowerChangePerformanceDao, PowerChangePerformance> {
    final PowerChangeMonitorConfigService powerChangeMonitorConfigService;
    final AccountUserService accountUserService;
    final AccountUserDao accountUserDao;
    final PowerChangePerformanceLogService powerChangePerformanceLogService;
    final ParkPointService parkPointService;
    final RedisService redisService;
    final PerformanceRuleGroupService ruleGroupService;

    /**
     * 分页查询详情
     */
    public PageResult<PowerChangePerformanceDetail> queryDetailPage(PowerChangePerformanceDetailReq req) {
        req.buildPowerRangeSqlCondition(powerChangeMonitorConfigService::getById);

        // 查询绩效分页
        Page<PowerChangePerformanceDetail> details = PageHelper.startPage(req.getPageNo(), req.getPageSize());
        getBaseMapper().queryDetailPage(req);
        details.close();

        PowerChangeMonitorConfig monitorConfig = Optional.ofNullable(powerChangeMonitorConfigService.getById(req.getAccountId()))
                .orElseGet(() -> new PowerChangeMonitorConfig().putDefaultValue(req.getAccountId()));
        Map<Integer, AccountUser> accountUserMap = Optional.ofNullable(accountUserService.getByType(req.getAccountId(), false))
                .map(x -> x.stream().collect(Collectors.toMap(AccountUser::getAccountUserId, Function.identity(), (v1, v2) -> v1)))
                .orElse(Collections.emptyMap());

        if (CollUtil.isNotEmpty(details)) {
            for (PowerChangePerformanceDetail detail : details) {
                // 补充运维人员信息
                AccountUser accountUser = accountUserMap.get(detail.getAccountUserId());
                if (accountUser != null) {
                    detail.setAccountUserName(accountUser.getName());
                    detail.setAccountUserPhone(accountUser.getPhone());
                }

                // 计算换电区间
                detail.setPowerRange(monitorConfig.matchPowerRange(detail.getOldSoc()));

                // 计算换电前后差值
                detail.setSocDiff(detail.getNewSoc() - detail.getOldSoc());
            }
        }

        return PageResult.convertPage(details.toPageInfo());
    }

    /**
     * 查询统计图表
     */
    public List<PerformanceChartPerDateDetail> queryStatChart(Integer accountId, Date startTime, Date endTime, Integer accountUserId, Integer machineType, Integer isValid, LoginInfo loginInfo) {
        List<PerformanceChartDo> chartDos = getBaseMapper().queryPerformanceChart(accountId, startTime, endTime, accountUserId, machineType, isValid);
        List<AccountUser> accountUserList = new ArrayList<>();
        if (accountUserId == null){
            accountUserList.addAll((List<AccountUser>) accountUserService.getParentsAccountUser(accountId, loginInfo).getData());
        }else {
            accountUserList.add(accountUserService.getAccountUserById(accountUserId));
        }
        Map<Integer, String> accountUserMap = Optional.ofNullable(accountUserList)
                .map(x -> x.stream().collect(Collectors.toMap(AccountUser::getAccountUserId, AccountUser::getName, (v1, v2) -> v1)))
                .orElse(Collections.emptyMap());
        return PerformanceChartPerDateDetail.fromDo(accountUserMap, chartDos, LocalDateTimeUtil.of(startTime).toLocalDate(), LocalDateTimeUtil.of(endTime).toLocalDate());
    }

    /**
     * 分页查询概览
     */
    public PageResult<PerformanceOverviewResp> queryOverviewPage(PerformanceOverviewReq req) {
        try (Page<PerformanceOverviewResp> page = PageHelper.startPage(req.getPageNo(), req.getPageSize())) {
            if (req.getType() == 0) { // 按人员查
                getBaseMapper().queryOverviewPageByUser(req);

                // 填充运维人员信息
                Map<Integer, AccountUser> accountUserMap = Optional.ofNullable(accountUserService.getByType(req.getAccountId(), false))
                        .map(x -> x.stream().collect(Collectors.toMap(AccountUser::getAccountUserId, Function.identity(), (v1, v2) -> v1)))
                        .orElse(Collections.emptyMap());
                if (CollUtil.isNotEmpty(page)) {
                    for (PerformanceOverviewResp overview : page) {
                        AccountUser accountUser = accountUserMap.get(overview.getAccountUserId());
                        if (accountUser != null) {
                            overview.setAccountUserName(accountUser.getName());
                            overview.setAccountUserPhone(accountUser.getPhone());
                        }
                    }
                }
            } else { // 按分区查
                getBaseMapper().queryOverviewPageByRegion(req);

                // 填充分区信息
                Map<Integer, Region> regionMap = accountUserDao.getRegionId(req.getAccountId()).stream()
                        .collect(Collectors.toMap(Region::getRegionId, Function.identity()));
                if (CollUtil.isNotEmpty(page)) {
                    for (PerformanceOverviewResp overview : page) {
                        Region region = regionMap.get(overview.getRegionId());
                        if (region != null) {
                            overview.setRegionName(region.getRegionName());
                        }
                    }
                }
            }
            return PageResult.convertPage(page.toPageInfo());
        }
    }

    /**
     * 更新绩效
     *
     * @param performanceId 绩效ID
     * @param performance   绩效值
     * @param remark        备注
     */
    @DS(DataSource.LOG)
    @Transactional
    public void updatePerformance(Long performanceId, BigDecimal performance, String remark) {
        PowerChangePerformance update = getById(performanceId);
        BigDecimal oldPerformance = update.getPerformance();
        update.setPerformance(performance);
        update.setRemark(remark);
        update.setPerformanceDetail("人工修改绩效: 总绩效" + NumberUtil.toStr(performance, true));
        update.setPerformanceType(PerformanceTypeConstant.MANUAL);
        updateById(update);
        // 计算新旧绩效差值，用于新增一条绩效日志记录power_change_performance_log
        BigDecimal subPerformance = performance.subtract(oldPerformance);
        if (subPerformance.compareTo(BigDecimal.ZERO) == 0){
            return;
        }
        // 记录绩效日志  业务类型设置为人工， 绩效为新旧绩效的差值
        PowerChangePerformanceLog log = new PowerChangePerformanceLog();
        BeanUtil.copyProperties(update, log, "remark");
        log.setPerformance(subPerformance);
        log.setRuleType(PerformanceRuleType.MANUAL.getValue());
        log.setRemark("MANAGER-人工修改绩效: " + NumberUtil.toStr(oldPerformance, true)+" -> "+ NumberUtil.toStr(performance, true));
        powerChangePerformanceLogService.save(log);
    }

    /**
     * 删除绩效
     *
     * @param powerChangeLogId 换电记录ID
     */
    @DS(DataSource.LOG)
    @Transactional
    public void removePerformance(Integer powerChangeLogId) {
        PowerChangePerformance powerChangePerformance = getOne(Wrappers.<PowerChangePerformance>query()
                .eq(PowerChangePerformance.Fields.powerChangeLogId, powerChangeLogId));
        if (powerChangePerformance == null) {
            return;
        }
        if (BigDecimal.ZERO.compareTo(powerChangePerformance.getPerformance()) != 0){
            PowerChangePerformanceLog log = new PowerChangePerformanceLog();
            BeanUtil.copyProperties(powerChangePerformance, log, "remark");
            log.setPerformance(powerChangePerformance.getPerformance().negate());// 取绩效负数
            log.setRuleType(PerformanceRuleType.MANUAL.getValue());
            log.setRemark("MANAGER-MANUAL-手动修改换电日志从有效改为无效-自动添加");
            powerChangePerformanceLogService.save(log);
        }
        removeById(powerChangePerformance.getPerformanceId());
    }

    /**
     * 插入一条0绩效记录
     *
     * @param logDb
     */
    public void insertZeroPerformance(PowerLogDetail logDb) {
        PowerChangePerformance performance = new PowerChangePerformance();
        performance.setPowerChangeLogId(logDb.getPowerChangeLogId());
        performance.setPerformance(BigDecimal.ZERO);
        performance.setPerformanceDetail("");
        performance.setOrderCountAfterChange(0);
        performance.setAccountId(logDb.getAccountId());
        performance.setFirstOrderTime(0);
        performance.setRemark("无效改有效系统自动生成;");
        save(performance);
    }

    @DS(DataSource.LOG)
    @Transactional()
    public void generatePerformance(PowerChangeLog powerChangeLog, BigDecimal performanceValue, AccountConfigExt performanceSwitch) {
        if (performanceValue != null && performanceValue.compareTo(BigDecimal.ZERO) < 0) {
            throw new BaseException("绩效值不能小于0");
        }
        // 获取绩效记录
        PowerChangePerformance powerChangePerformance = getOne(Wrappers.<PowerChangePerformance>query()
                .eq(PowerChangePerformance.Fields.powerChangeLogId, powerChangeLog.getPowerChangeLogId())
        );
        if (powerChangePerformance == null) {
            log.info("[换电绩效] 生成新绩效记录, 换电记录id: {},performanceValue:{}", powerChangeLog.getPowerChangeLogId(), performanceValue);
            powerChangePerformance = createPowerChangePerformance(powerChangeLog);
            if (performanceValue != null){
                powerChangePerformance.setPerformance(performanceValue);
                powerChangePerformance.setPerformanceDetail("manager-修改有效性同时修改绩效");
                // 设置人工修改的标志，被人工修改之后，后面的绩效计算不再考虑这条记录。
                powerChangePerformance.setPerformanceType(PerformanceTypeConstant.MANUAL);
                save(powerChangePerformance);
                // 记录绩效日志  业务类型设置为人工， 绩效为新旧绩效的差值
                PowerChangePerformanceLog log = new PowerChangePerformanceLog();
                BeanUtil.copyProperties(powerChangePerformance, log, "remark");
                log.setPerformance(performanceValue);
                log.setPerformanceDetail("manager-人工修改绩效值");
                log.setRuleType(PerformanceRuleType.MANUAL.getValue());
                log.setRemark("manager-修改有效性同时修改绩效: " + NumberUtil.toStr(performanceValue, true));
                powerChangePerformanceLogService.save(log);
            }else {
                List<PowerChangePerformanceLog> performanceLogs = new ArrayList<>();
                // 通过规则计算绩效值
                calculatePerformance(powerChangeLog, powerChangePerformance, performanceLogs, performanceSwitch);
                save(powerChangePerformance);
                // 保存绩效日志
                if (!performanceLogs.isEmpty()) {
                    // 填充绩效记录id
                    Long performanceId = powerChangePerformance.getPerformanceId();
                    performanceLogs.forEach(performanceLog -> performanceLog.setPerformanceId(performanceId));
                    powerChangePerformanceLogService.insertBatch(performanceLogs);
                }
            }
        }else {
            // 已经存在绩效记录
            if (performanceValue != null) {
                log.info("[换电绩效] 已经存在绩效记录,修改绩效, 换电记录id: {},绩效记录id: {},performanceValue:{}", powerChangeLog.getPowerChangeLogId(), powerChangePerformance.getPerformanceId(), performanceValue);
                BigDecimal oldPerformance = powerChangePerformance.getPerformance();
                // 需要修改绩效值
                powerChangePerformance.setPerformance(performanceValue);
                powerChangePerformance.setPerformanceDetail("manager-修改有效性同时修改绩效");
                // 设置人工修改的标志，被人工修改之后，后面的绩效计算不再考虑这条记录。
                powerChangePerformance.setPerformanceType(PerformanceTypeConstant.MANUAL);
                // 计算新旧绩效差值，用于新增一条绩效日志记录power_change_performance_log
                BigDecimal subPerformance = performanceValue.subtract(oldPerformance);
                if (subPerformance.compareTo(BigDecimal.ZERO) != 0) {
                    // 记录绩效日志  业务类型设置为人工， 绩效为新旧绩效的差值
                    PowerChangePerformanceLog log = new PowerChangePerformanceLog();
                    BeanUtil.copyProperties(powerChangePerformance, log, "remark");
                    log.setPerformance(subPerformance);
                    log.setRuleType(PerformanceRuleType.MANUAL.getValue());
                    log.setRemark("manager-修改有效性同时修改绩效: " + NumberUtil.toStr(oldPerformance, true) + " -> " + NumberUtil.toStr(performanceValue, true));
                    powerChangePerformanceLogService.save(log);
                }
                updateById(powerChangePerformance);
            }
        }
    }

    /**
     * 计算并插入绩效
     *
     * @param pcl                    换电记录
     * @param powerChangePerformance
     * @param performanceLogs
     * @param performanceSwitch
     */
    private void calculatePerformance(PowerChangeLog pcl, PowerChangePerformance powerChangePerformance, List<PowerChangePerformanceLog> performanceLogs, AccountConfigExt performanceSwitch) {
        switch (performanceSwitch.getParamValue()) {
            case "1":
                calculateConventionModePerformance(pcl, powerChangePerformance, performanceLogs);
                break;
            case "2":
                calculateStationModePerformance(powerChangePerformance, performanceLogs);
                break;
        }
    }

    /**
     * 计算换电常规方案绩效
     */
    private void calculateConventionModePerformance(PowerChangeLog pcl, PowerChangePerformance pcp, List<PowerChangePerformanceLog> performanceLogs) {
        PowerChangePerformanceConfig config = getPowerChangePerformanceConfig(pcp.getAccountId(), PerformanceBizType.POWER_CHANGE_CONVENTION);
        log.info("[换电绩效] 计算常规方案,换电记录id: {}", pcp.getPowerChangeLogId());

        BigDecimal performance = BigDecimal.ZERO;
        StringBuilder detail = new StringBuilder();

        // 基础绩效
        if (config.getConventionBasic() != null && config.getConventionBasic().getEnabled()) {
            log.info("[换电绩效] 换电记录id: {}, 适用常规方案基础绩效", pcp.getPowerChangeLogId());
            BigDecimal validPerf = config.getConventionBasic().getValidPerformance();
            performance = performance.add(validPerf);
            detail.append("常规方案基础绩效: ")
                    .append(NumberUtil.toStr(validPerf))
                    .append(";");
            PowerChangePerformanceLog performanceLog = new PowerChangePerformanceLog();
            BeanUtil.copyProperties(pcp, performanceLog, "performance", "performanceDetail", "remark");
            performanceLog.setPerformance(validPerf);
            performanceLog.setPerformanceDetail("常规方案基础绩效: " + NumberUtil.toStr(validPerf) + ";");
            performanceLog.setRuleType(PerformanceRuleType.BASIC.getValue());
            performanceLog.setRemark("MANAGER-BASIC-常规方案基础");
            performanceLogs.add(performanceLog);
        }

        // 低电优先换电
        if (config.getLowPowerBased() != null && config.getLowPowerBased().getEnabled()) {
            for (LowPowerRule subRule : config.getLowPowerBased().getSubRules()) {
                if (subRule.getBeforeChangePower().match(pcl.getOldSoc())) {
                    log.info("[换电绩效] 换电记录id: {}, 适用低电优先换电绩效, 换电前电量范围: {}", pcp.getPowerChangeLogId(), subRule.getBeforeChangePower());
                    performance = performance.add(subRule.getPerformance());
                    detail.append("低电优先绩效: ")
                            .append(NumberUtil.toStr(subRule.getPerformance()))
                            .append("(电量百分比: ")
                            .append(subRule.getBeforeChangePower())
                            .append(");");
                    PowerChangePerformanceLog performanceLog = new PowerChangePerformanceLog();
                    BeanUtil.copyProperties(pcp, performanceLog, "performance", "performanceDetail", "remark");
                    performanceLog.setPerformance(subRule.getPerformance());
                    performanceLog.setPerformanceDetail("低电优先绩效: " + NumberUtil.toStr(subRule.getPerformance()) + "(电量百分比: " + subRule.getBeforeChangePower() + ");");
                    performanceLog.setRuleType(PerformanceRuleType.LOW_POWER.getValue());
                    performanceLog.setRemark("MANAGER-LOW_POWER-低电优先");
                    performanceLogs.add(performanceLog);
                    break;
                }
            }
        }

        // 换电后可用电量
        if (config.getPowerAfterChangeBased() != null && config.getPowerAfterChangeBased().getEnabled()) {
            for (PowerAfterChangeRule subRule : config.getPowerAfterChangeBased().getSubRules()) {
                if (subRule.getAfterChangePower().match(pcl.getNewSoc())) {
                    log.info("[换电绩效] 换电记录id: {}, 适用换电后可用电量绩效, 换电后电量范围: {}", pcp.getPowerChangeLogId(), subRule.getAfterChangePower());
                    performance = performance.add(subRule.getPerformance());
                    detail.append("换电后可用电量绩效: ")
                            .append(NumberUtil.toStr(subRule.getPerformance()))
                            .append("(电量百分比: ")
                            .append(subRule.getAfterChangePower())
                            .append(");");
                    PowerChangePerformanceLog performanceLog = new PowerChangePerformanceLog();
                    BeanUtil.copyProperties(pcp, performanceLog, "performance", "performanceDetail", "remark");
                    performanceLog.setPerformance(subRule.getPerformance());
                    performanceLog.setPerformanceDetail("换电后可用电量绩效: " + NumberUtil.toStr(subRule.getPerformance()) + "(电量百分比: " + subRule.getAfterChangePower() + ");");
                    performanceLog.setRuleType(PerformanceRuleType.POWER_AFTER_CHANGE.getValue());
                    performanceLog.setRemark("MANAGER-POWER_AFTER_CHANGE-换电后可用电量");
                    performanceLogs.add(performanceLog);
                    break;
                }
            }
        }

        pcp.setPerformance(performance);
        pcp.setPerformanceDetail(detail.toString());
    }

    /**
     * 计算换电站点优先方案绩效
     */
    private void calculateStationModePerformance(PowerChangePerformance pcp, List<PowerChangePerformanceLog> performanceLogs) {
        PowerChangePerformanceConfig config = getPowerChangePerformanceConfig(pcp.getAccountId(), PerformanceBizType.POWER_CHANGE_STATION);
        log.info("[换电绩效] 计算站点优先方案,换电记录id: {}", pcp.getPowerChangeLogId());

        BigDecimal performance = BigDecimal.ZERO;
        StringBuilder detail = new StringBuilder();

        // 基础绩效
        if (config.getStationBasic() != null && config.getStationBasic().getEnabled()) {
            log.info("[换电绩效] 换电记录id: {}, 适用站点优先方案基础绩效", pcp.getPowerChangeLogId());
            BigDecimal validPerf = config.getStationBasic().getValidPerformance();
            performance = performance.add(validPerf);
            detail.append("站点优先方案基础绩效: ")
                    .append(NumberUtil.toStr(validPerf))
                    .append(";");
            PowerChangePerformanceLog performanceLog = new PowerChangePerformanceLog();
            BeanUtil.copyProperties(pcp, performanceLog, "performance", "performanceDetail", "remark");
            performanceLog.setPerformance(validPerf);
            performanceLog.setPerformanceDetail("站点优先方案基础绩效: " + NumberUtil.toStr(validPerf) + ";");
            performanceLog.setRuleType(PerformanceRuleType.BASIC.getValue());
            performanceLog.setRemark("MANAGER-BASIC-站点优先方案");
            performanceLogs.add(performanceLog);
        }

        // 站点优先级
        sp:
        if (config.getStationPriorityBased() != null && config.getStationPriorityBased().getEnabled() && pcp.getParkId() != null) {
            ParkPoint parkPoint = parkPointService.getByParkPointId(pcp.getParkId());
            if (parkPoint == null) {
                break sp;
            }

            for (StationPriorityBasedRule subRule : config.getStationPriorityBased().getSubRules()) {
                if (Objects.equals(parkPoint.getLevel(), subRule.getLevel())) {
                    log.info("[换电绩效] 换电记录id: {}, 适用站点优先级绩效, 站点名称: {}, 优先级: {}", pcp.getPowerChangeLogId(), parkPoint.getName(), parseParkPointLevel(parkPoint.getLevel()));
                    performance = performance.add(subRule.getPerformance());
                    detail.append("站点优先级绩效: ")
                            .append(NumberUtil.toStr(subRule.getPerformance()))
                            .append("(")
                            .append(parseParkPointLevel(parkPoint.getLevel()))
                            .append(");");
                    PowerChangePerformanceLog performanceLog = new PowerChangePerformanceLog();
                    BeanUtil.copyProperties(pcp, performanceLog, "performance", "performanceDetail", "remark");
                    performanceLog.setPerformance(subRule.getPerformance());
                    performanceLog.setPerformanceDetail("站点优先级绩效: " + NumberUtil.toStr(subRule.getPerformance()) + "(" + parseParkPointLevel(parkPoint.getLevel()) + ");");
                    performanceLog.setRuleType(PerformanceRuleType.STATION_PRIORITY.getValue());
                    performanceLog.setRemark("MANAGER-STATION_PRIORITY-站点优先级：" + parseParkPointLevel(parkPoint.getLevel()));
                    performanceLogs.add(performanceLog);
                }
            }
        }

        pcp.setPerformance(performance);
        pcp.setPerformanceDetail(detail.toString());
    }

    private PowerChangePerformance createPowerChangePerformance(PowerChangeLog pcl) {
        PowerChangePerformance pcp = new PowerChangePerformance();
        pcp.setPowerChangeLogId(pcl.getPowerChangeLogId());
        pcp.setOrderCountAfterChange(0);
        pcp.setAccountId(pcl.getAccountId());

        // 缓存取站点
        // 如果换电后首单时效过长（7天）则无法获取到站点信息
        Integer parkPointId = getParkPointCache(pcl.getMachineId());
        if (parkPointId != null) {
            ParkPoint parkPoint = parkPointService.getByParkPointId(parkPointId);
            if (parkPoint != null) {
                pcp.setParkId(parkPoint.getParkPointId());
                pcp.setParkName(parkPoint.getName());
            }
        }
        return pcp;
    }

    /**
     * 解析站点等级
     */
    private static String parseParkPointLevel(int level) {
        switch (level) {
            case 1:
                return "热门一级";
            case 2:
                return "热门二级";
            case 3:
                return "普通站点";
            case 4:
                return "冷门二级";
            case 5:
                return "冷门一级";
            default:
                return "未知";
        }
    }

    /**
     * 获取车辆当前所在站点
     *
     * @param machineId 车辆id
     * @return 所在站点id
     */
    public Integer getParkPointCache(Integer machineId) {
        String parkPointId = redisService.get(MACHINE_PARK_POINT_KEY + machineId);
        if (parkPointId != null) {
            return Integer.parseInt(parkPointId);
        }
        return null;
    }

    public PowerChangePerformanceConfig getPowerChangePerformanceConfig(Integer accountId, PerformanceBizType bizType){
        return ruleGroupService.selectBizPerformanceConfig(accountId, PowerChangePerformanceConfig::fromRaw, bizType);
    }

    public static final String MACHINE_PARK_POINT_KEY = "maintainPerformance:powerChange:parkPoint:";
}
